<?php

class Webservice_Model_Logic extends Zend_Db_Table {
    
    const TABLE_CATEGORY = "tblcategory";
    const TABLE_SHOP = "tblShopManager";
    const TABLE_GOOD = "tblgoods";
    const OK = 0;
    const NOT_OK = -1;
    
    public function getCategoryList(&$aryCategory, $params) {
        
        $result = self::OK;
        $where = "1=1";
        if($params['id']) {
            $where = $this->_db->quoteInto("CategoryID = ?", $params['id']);
        }
        $sql = $this->_db->select()
                         ->from(self::TABLE_CATEGORY)
                         ->where($where);
                         
        if(!$params['id']) {
            $sql->limit($params['limit'], $params['from']);
        }
        try{
            $aryCategory = $this->_db->fetchAll($sql);
        } catch (Zend_Db_Exception $e) {
            $result = self::NOT_OK;
        }
        return $result;
    }
    public function getShopList(&$aryShop, $params) {
        
        $result = self::OK;
        $where = "1=1";
        if($params['id']) {
            $where = $this->_db->quoteInto("ShopID = ?", $params['id']);
        }
        $sql = $this->_db->select()
                         ->from(self::TABLE_SHOP)
                         ->where($where);
                         
        if(!$params['id']) {
            $sql->limit($params['limit'], $params['from']);
        }
        try{
            $aryShop = $this->_db->fetchAll($sql);
        } catch (Zend_Db_Exception $e) {
            $result = self::NOT_OK;
        }
        return $result;
    }
    public function getGoodList(&$aryGood, $params) {
        
        $result = self::OK;
        $where = "1=1";
        if($params['id']) {
            $where = $this->_db->quoteInto("GoodID = ?", $params['id']);
        }
        $sql = $this->_db->select()
                         ->from(self::TABLE_GOOD)
                         ->where($where);
        if(!$params['id']) {
            $sql->limit($params['limit'], $params['from']);
        }
        try{
            $aryGood = $this->_db->fetchAll($sql);
        } catch (Zend_Db_Exception $e) {
            $result = self::NOT_OK;
        }
        return $result;
    }
    public function searchShop(&$aryShop, $params) {
        
        $result = self::OK;
        $where = "1=1";
        if($params['keyword']) {
            $where = $this->_db->quoteInto("ShopName LIKE ?", '%'.$params['keyword'].'%');
        }
        $sql = $this->_db->select()
                         ->from(self::TABLE_SHOP)
                         ->where($where);
        $sql->limit($params['limit'], $params['from']);
        try{
            $aryShop = $this->_db->fetchAll($sql);
        } catch (Zend_Db_Exception $e) {
            $result = self::NOT_OK;
        }
        return $result;
    }
    public function searchGood(&$aryGood, $params) {
        
        $result = self::OK;
        $where = "1=1";
        if($params['keyword']) {
            $where = $this->_db->quoteInto("GoodName LIKE ?", '%'.$params['keyword'].'%');
        }
        $sql = $this->_db->select()
                         ->from(self::TABLE_GOOD)
                         ->where($where);
        $sql->limit($params['limit'], $params['from']);
        try{
            $aryGood = $this->_db->fetchAll($sql);
        } catch (Zend_Db_Exception $e) {
            $result = self::NOT_OK;
        }
        return $result;
    }
}